import pygame
from pygame.locals import *
#import Animation
from Animation import *

class GameObject(pygame.sprite.Sprite):

    RIGHT = 0
    LEFT = 1
    
    BLANK_ANIMATION_ENTRY = (0, None, 0, Animation.NO_KEY_FRAME)
    BLANK_SOUND_ENTRY = (0, None)
    
    DEFAULT_ANIMATI0N = 0
    DEFAULT_POSITION = (0,0)
    RECT_SIZE = (0,0)
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.animations = []

        self.currentAnimation = self.DEFAULT_ANIMATION

        self.image = pygame.Surface(GameObject.RECT_SIZE) 
        self.rect = Rect(self.DEFAULT_POSITION, self.RECT_SIZE)
        self.facing = self.RIGHT
        
    def setPosition(self,x,y):
        self.rect.topleft = (x,y)

    #generically loads all animation listed in animation table of subclasses
    def linkAnimations(self, rManager,fps):
        self.animations = range(self.NUM_ANIMATIONS *2)
        for (index, name, fps_factor, keyFrame) in self.ANIMATION_TABLE:
            if name != None:
                self.animations[index] = Animation(rManager.getAnimation(name),fps/fps_factor, keyFrame)

    #generically loads all sounds listed in animation table of subclasses
    def linkSounds(self,rManager):
        self.sounds = range(self.NUM_SOUNDS)
        for (index, name) in self.SOUND_TABLE:
            if name != None:
                self.sounds[index] = rManager.getSound(name)


    def updateCurrentImage(self):
        self.image = self.animations[self.currentAnimation].image
 

    def resetAnimationFrame(self):
        self.animations[self.currentAnimation].currentFrame = 0


    def isActionComplete(self, action):
        return self.isActing(action) and self.animations[self.currentAnimation].isComplete()

    def isActionActive(self,action):
        return self.isActing(action) and self.animations[self.currentAnimation].isKeyFrame()
    
    def isActing(self,action):
        return self.currentAnimation - self.facing == action
  
        
def gameObjectPosCompare(gameObject1, gameObject2):
   return gameObject1.rect.top - gameObject2.rect.top
